﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using SharePoint.Contrib.Extensions;
using Microsoft.SharePoint;

namespace NUnit.SharePointContrib
{
    [TestFixture]
    public class SPContentTypeExtensionsTest
    {
        SPSite sitecollection = null;
        SPWeb root = null;
        SPContentType custom;

        [SetUp]
        public void Init()
        {
            if (SharePoint.TryFindSiteCollection(out sitecollection))
            {
                root = sitecollection.RootWeb;
                
                SPContentType task = root.ContentTypes[SPBuiltInContentTypeId.Task];
                custom = new SPContentType(task, root.ContentTypes, "CustomTask");
                custom = root.ContentTypes.Add(custom);
                root.Update();
            }
        }

        [TearDown]
        public void Dispose()
        {
            root.ContentTypes.Delete(custom.Id);
            root.Update();
            sitecollection.Dispose();
        }
        
        [Test]
        public void AddFieldRefCanAddFieldToContentTypeUsingName()
        {
            Assert.That(custom.Fields.Contains("HomePhone"), Is.False);            
            custom.AddFieldRef("HomePhone");
            Assert.That(custom.Fields.Contains("HomePhone"), Is.True);
        }

        [Test]
        public void AddFieldRefCanAddFieldToContentTypeUsingID()
        {
            Assert.That(custom.Fields.Contains(SPBuiltInFieldId.WorkAddress), Is.False);
            custom.AddFieldRef(SPBuiltInFieldId.WorkAddress);
            Assert.That(custom.Fields.Contains(SPBuiltInFieldId.WorkAddress), Is.True);
        }
    }
}
